"""
Case Type   : 兼容M*数据库time函数
Case Name   : dolphin.b_compatibility_mode为true下，测试time函数
Create At   : 2023.11.20
Owner       : zhanghuan96
Description :
    1、入参为微秒或特殊值
    2、入参分或秒超过60
    3、清理环境
Expect      :
    1、成功
    2、有告警，返回null
    3、成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest11(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0011"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:入参为微秒或特殊值;expect:成功--'
        self.log.info(text)
        sql_cmd = "set dolphin.b_compatibility_mode = true;" \
                  "select time('23-65');" \
                  "select time('23:59:59.8888');" \
                  "select time('23:55:56.1234');" \
                  "select time('0:0:0');" \
                  "select time('-1:-1:-1');" \
                  "reset dolphin.b_compatibility_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        msg_list = ['00:00:23', '23:59:59.8888', '23:55:56.1234', '00:00:00',
                    ' -00:00:01']
        for msg in msg_list:
            self.assertIn(msg, msg_list, '执行失败:' + text)

        text = "--step2:入参分或秒超过60;expect:有告警，返回null--"
        self.log.info(text)
        sql_cmd = "set dolphin.b_compatibility_mode = true;" \
                  "select time('23:65:66');" \
                  "select time('23:65');" \
                  "select time('23:65:66.8888');" \
                  "reset dolphin.b_compatibility_mode;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count('time \n------\n \n(1 row)'),
                                          '执行失败' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
